import {
  getSpaceList
} from '../../services/inspect-point'
import {
  checkIfLogin
} from '../../utils/check'

Page({
  data: {
    latitude: 31.99226,
    longitude: 118.7787,
    markers: [{
      id: 1,
      latitude: 31.99226,
      longitude: 118.7787,
      callout: {
        content: '新华汇'
      }
    }],
    pointList:[]
  },

  onLoad() {
    checkIfLogin()
    this.handleGetSpaceList()
    // wx.getLocation({
    //   type: 'gcj02', //返回可以用于wx.openLocation的经纬度
    //   success: (res) => {
    //     const latitude = res.latitude
    //     const longitude = res.longitude
    //     const markers = []
    //     markers.push({
    //         id: 1,
    //         latitude: latitude,
    //         longitude: longitude,
    //         callout: {
    //           content: '新华汇'
    //         }
    //       })
    //     this.setData({
    //       latitude,
    //       longitude,
    //       markers
    //     })
    //     // console.log("latitude,longitude>>",latitude,longitude)
    //     // wx.openLocation({
    //     //   latitude,
    //     //   longitude,
    //     //   scale: 18
    //     // })
    //   }
    //  })
  },

  onShow() {
    checkIfLogin()
    this.handleGetSpaceList()
  },

  onRouteDone() {
    this.handleGetSpaceList()
  },

  async handleGetSpaceList() {
    let res = await getSpaceList()
    if (res.code === 200) {
      this.getLongLat(res.rows)
      this.getOpeningHours(res.rows)
      this.setData({
        pointList: res.rows
      })
    }
  },

  getLongLat(list) {
    let tempMakerList = []
    for (let i = 0; i < list.length; i++) {
      let item = list[i]
      const longLatItem = item.sysSpaceBasicList.find(item => item.url === 'long_lat')
      if (!longLatItem.value) {
        return
      }
      const longLatValue = longLatItem.value.split(',')
      if (longLatValue.length) {
        item.longLatInfo = {
          longitude: parseFloat(longLatValue[0]),
          latitude: parseFloat(longLatValue[1])
        }
        let tempMakerItem = {
          id: i,
          longitude: parseFloat(longLatValue[0]),
          latitude: parseFloat(longLatValue[1]),
          // width: 40,
          // height: 60,
          callout: {
            content: item.name
          }
        }
        tempMakerList.push(tempMakerItem)
      }
    }
    if (tempMakerList.length) {
      const firstItem = tempMakerList[0]
      this.setData({
        latitude: firstItem.latitude,
        longitude: firstItem.longitude,
      })
    }
    this.setData({
      markers: tempMakerList
    })
  },

  getOpeningHours(list) {
    for (let item of list) {
      const openingHoursItem = item.sysSpaceBasicList.find(item => item.url === 'openingHours')
      item.openingHours = openingHoursItem ? openingHoursItem.value : ""
    }
  },

  mapToPlace(event) {
    const itemInfo = event.currentTarget.dataset.iteminfo
    if (itemInfo.longLatInfo) {
      wx.openLocation({
        ...itemInfo.longLatInfo,
        scale: 18
      })
    }
  },

  confirmStation(event) {
    const stationId = event.currentTarget.dataset.itemid
    const stationName = event.currentTarget.dataset.itemname
    const app = getApp()
    app.globalData.globalInfo.stationId = stationId
    app.globalData.globalInfo.stationName = stationName

    wx.navigateTo({
      url: '/pages/inspection-project/inspection-project',
    })
  }
})